package com.xiechur.listener;

import com.xiechur.service.RepairServiceI;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.*;
import javax.servlet.http.*;

/**
 * 在系统启动时，修复数据库
 *
 *
 */
public class RepairListener implements ServletContextListener, ServletContextAttributeListener, HttpSessionListener, HttpSessionAttributeListener, HttpSessionActivationListener, HttpSessionBindingListener, ServletRequestListener, ServletRequestAttributeListener {
    private static final Logger logger = Logger.getLogger(RepairListener.class);

    private static ApplicationContext applicationContext = null;

    public RepairListener() {
    }

    public void requestDestroyed(ServletRequestEvent arg0) {
    }

    public void attributeAdded(HttpSessionBindingEvent evt) {
    }

    public void contextInitialized(ServletContextEvent evt) {
        logger.info("修复数据库");
        applicationContext = WebApplicationContextUtils.getWebApplicationContext(evt.getServletContext());
        RepairServiceI repairService = (RepairServiceI) applicationContext.getBean("repairService");
        repairService.repair();
    }

    public void sessionDidActivate(HttpSessionEvent arg0) {
    }

    public void valueBound(HttpSessionBindingEvent arg0) {
    }

    public void attributeAdded(ServletContextAttributeEvent arg0) {
    }

    public void attributeRemoved(ServletContextAttributeEvent arg0) {
    }

    public void sessionDestroyed(HttpSessionEvent evt) {
    }

    public void attributeRemoved(HttpSessionBindingEvent arg0) {
    }

    public void attributeAdded(ServletRequestAttributeEvent evt) {
    }

    public void valueUnbound(HttpSessionBindingEvent arg0) {
    }

    public void sessionWillPassivate(HttpSessionEvent arg0) {
    }

    public void sessionCreated(HttpSessionEvent arg0) {
    }

    public void attributeReplaced(HttpSessionBindingEvent arg0) {
    }

    public void attributeReplaced(ServletContextAttributeEvent arg0) {
    }

    public void attributeRemoved(ServletRequestAttributeEvent arg0) {
    }

    public void contextDestroyed(ServletContextEvent evt) {
    }

    public void attributeReplaced(ServletRequestAttributeEvent arg0) {
    }

    public void requestInitialized(ServletRequestEvent arg0) {
    }

}
